package com.supermap.common;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLDecoder;

/**
 * Created by xiaolu on 2017/8/11.
 */
public class HttpRequestUtil {
    public static String SMS_URL = "http://45.77.19.115:8099/sms/";

    public static String PRODUCT_COUNT_URL = "tBPmsProductController.do?getProductCountByArea";

    public static String PRODUCT_DETIAL_COUNT_URL = "tBPmsProductController.do?getProductDetialsByArea";

    public static String PRODUCT_PUBLISH_COUNT_URL = "tBPmsProductController.do?getProductPublishMonthlyByArea";

    private static Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);    //日志记录

    /**
     * post请求
     *
     * @param url       url地址
     * @param jsonParam 参数
     * @return getStatusCode
     */
    public static int httpPost(String url, JSONObject jsonParam){
        //post请求返回结果
        DefaultHttpClient httpClient = new DefaultHttpClient();
        int statusCode = 404;
        HttpPost method = new HttpPost(url);
        try {
            if (null != jsonParam) {
                //解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse httpResponse = httpClient.execute(method);
            url = URLDecoder.decode(url, "UTF-8");

            //请求发送成功，并得到响应
            statusCode = httpResponse.getStatusLine().getStatusCode();

        } catch (IOException e) {
            logger.error("post请求提交失败:" + url, e);
        }
        return statusCode;
    }

    /**
     * 发送get请求
     *
     * @param url 路径
     * @return
     */
    public static JSONObject httpGet(String url, JSONObject jsonParam) {
        //get请求返回结果
        JSONObject jsonResult = null;
        for (String key:jsonParam.keySet()) {
            url = url + "&" + key + "=" + jsonParam.get(key);
        }
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                /**把json字符串转换成json对象**/
                jsonResult = JSONObject.parseObject(strResult);
                url = URLDecoder.decode(url, "UTF-8");
            } else {
                logger.error("get请求提交失败:" + url);
            }
        } catch (IOException e) {
            logger.error("get请求提交失败:" + url, e);
        }
        return jsonResult;
    }
}
